package com.learn.project2.service.admin.config;

import com.learn.project2.mapper.MarketSystemMapper;
import com.learn.project2.model.vo.admin.config.ConfigExpressVO;
import com.learn.project2.model.vo.admin.config.ConfigMallVO;
import com.learn.project2.model.vo.admin.config.ConfigOrderVO;
import com.learn.project2.model.vo.admin.config.ConfigWxVO;
import com.learn.project2.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.net.ssl.SNIMatcher;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: 尹圣淼
 * @Date: 2022/1/7 16:36
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public ConfigMallVO mallShow() {
        ConfigMallVO configMallVO = new ConfigMallVO();

//        String [] names =
//                {"market_mall_longitude","market_mall_latitude","market_mall_address",
//                ,"market_mall_qq","market_mall_phone", "market_mall_name"};
//        for (String name : names) {
//        }

        String longitude = marketSystemMapper.selectValueByName("market_mall_longitude");
        configMallVO.setMarket_mall_longitude(longitude);
        String latitude = marketSystemMapper.selectValueByName("market_mall_latitude");
        configMallVO.setMarket_mall_latitude(latitude);
        String mallAddress = marketSystemMapper.selectValueByName("market_mall_address");
        configMallVO.setMarket_mall_address(mallAddress);
        String mallQq = marketSystemMapper.selectValueByName("market_mall_qq");
        configMallVO.setMarket_mall_qq(mallQq);
        String mallPhone = marketSystemMapper.selectValueByName("market_mall_phone");
        configMallVO.setMarket_mall_phone(mallPhone);
        String mallName = marketSystemMapper.selectValueByName("market_mall_name");
        configMallVO.setMarket_mall_name(mallName);
        return configMallVO;
    }

    @Override
    public Result mallUpdate(ConfigMallVO configMallVO) {
        // 执行update方法。
        // 根据主键进行修改。（反正主键和name都不会被修改）
        //  Integer[] keys = {7,16,13,3,14,9};
        marketSystemMapper.updateValueById(7, configMallVO.getMarket_mall_name());
        marketSystemMapper.updateValueById(16, configMallVO.getMarket_mall_address());
        // 左面的坐标。
        String regexOfNum = "^\\d+(\\.\\d+)?$|^\\d+$";
        Pattern patternNum = Pattern.compile(regexOfNum);
        Matcher matcher1 = patternNum.matcher(configMallVO.getMarket_mall_longitude());
        Matcher matcher2 = patternNum.matcher(configMallVO.getMarket_mall_latitude());
        if (!matcher1.matches() || !matcher2.matches()) {
            return Result.fail("坐标格式不对，请输入正确的非负数值。");
        }

//        try {
//
//            double longitude = Double.parseDouble(configMallVO.getMarket_mall_longitude());
//            marketSystemMapper.updateValueById(13, configMallVO.getMarket_mall_longitude());
//            // 右面的坐标。
//            double latitude = Double.parseDouble(configMallVO.getMarket_mall_latitude());
//            marketSystemMapper.updateValueById(3, configMallVO.getMarket_mall_latitude());
//        } catch (NumberFormatException e) {
//            return Result.fail("坐标格式不对，请输入正确的非负浮点数数值。");
//        }

        String regexOfPhone = "0\\d{2,3}[-]?\\d{7,8}|0\\d{2,3}\\s?\\d{7,8}|13[0-9]\\d{8}|15[1089]\\d{8}";
        Pattern patternPhone = Pattern.compile(regexOfPhone);
        Matcher matcherPhone = patternPhone.matcher(configMallVO.getMarket_mall_phone());
        if (!matcherPhone.matches()) { // 不符合正则表达式。
            return Result.fail("输入的电话号码格式有误，请重新输入。");
        }
        marketSystemMapper.updateValueById(14, configMallVO.getMarket_mall_phone());
        String regexOfQQ = "[1-9]\\d{0,14}"; // 1-14位。
        Pattern patternQQ = Pattern.compile(regexOfQQ);
        Matcher matcherQQ = patternQQ.matcher(configMallVO.getMarket_mall_qq());
        if (!matcherQQ.matches()) {
            return Result.fail("输入的QQ号码格式有误，请重新输入。");
        }
        marketSystemMapper.updateValueById(9, configMallVO.getMarket_mall_qq());
        return Result.ok(configMallVO);
    }

    @Override
    public ConfigExpressVO expressShow() {
        // 直接根据id进行查找。-> 主键
        String freightMin = marketSystemMapper.selectValueById(6);
        String freightValue = marketSystemMapper.selectValueById(8);
        ConfigExpressVO configExpressVO = new ConfigExpressVO();
        configExpressVO.setMarket_express_freight_min(freightMin);
        configExpressVO.setMarket_express_freight_value(freightValue);
        return configExpressVO;
    }

    @Override
    public Result expressUpdate(ConfigExpressVO configExpressVO) {
        // 直接更新数据即可。
        String regex = "^\\d+(\\.\\d+)?$|^\\d+$";
        Pattern patternNum = Pattern.compile(regex);
        Matcher matcherMin = patternNum.matcher(configExpressVO.getMarket_express_freight_min());
        Matcher matcherValue = patternNum.matcher(configExpressVO.getMarket_express_freight_value());
        if (!matcherMin.matches()) {
            return Result.fail("最低消费金额格式不对，请输入正确的非负数值");
        }
        if (!matcherValue.matches()) {
            return Result.fail("满减不足所需运费金额格式不对,请输入正确的非负数值。");
        }
        double freightMin = Double.parseDouble(configExpressVO.getMarket_express_freight_min());
        marketSystemMapper.updateValueById(6, configExpressVO.getMarket_express_freight_min());
        double freightValue = Double.parseDouble(configExpressVO.getMarket_express_freight_value());
        marketSystemMapper.updateValueById(8, configExpressVO.getMarket_express_freight_value());
        return Result.ok(configExpressVO);
    }

    @Override
    public ConfigOrderVO orderShow() {
        String unpaid = marketSystemMapper.selectValueById(1);
        String unconfirm = marketSystemMapper.selectValueById(4);
        String comment = marketSystemMapper.selectValueById(11);
        ConfigOrderVO configOrderVO = new ConfigOrderVO();
        configOrderVO.setMarket_order_unconfirm(unconfirm);
        configOrderVO.setMarket_order_comment(comment);
        configOrderVO.setMarket_order_unpaid(unpaid);
        return configOrderVO;
    }

    @Override
    public Result orderUpdate(ConfigOrderVO configOrderVO) {

        // ^\d+$
        String regexNum = "^\\d+$"; //
        Pattern pattern = Pattern.compile(regexNum);
        Matcher matcher1 = pattern.matcher(configOrderVO.getMarket_order_unpaid());
        Matcher matcher2 = pattern.matcher(configOrderVO.getMarket_order_comment());
        Matcher matcher3 = pattern.matcher(configOrderVO.getMarket_order_unconfirm());
        if (!matcher1.matches() || !matcher2.matches() || !matcher3.matches()) {
            return Result.fail("格式有误，请输入非负整数形式。");
        }

        marketSystemMapper.updateValueById(1, configOrderVO.getMarket_order_unpaid());
        marketSystemMapper.updateValueById(4, configOrderVO.getMarket_order_unconfirm());
        marketSystemMapper.updateValueById(11, configOrderVO.getMarket_order_comment());
        return Result.ok(configOrderVO);
    }

    @Override
    public ConfigWxVO wxShow() {
        ConfigWxVO configWxVO = new ConfigWxVO();
        String newIndex = marketSystemMapper.selectValueById(2);
        configWxVO.setMarket_wx_index_new(newIndex);
        String hotIndex = marketSystemMapper.selectValueById(10);
        configWxVO.setMarket_wx_index_hot(hotIndex);
        String goodsLog = marketSystemMapper.selectValueById(12);
        configWxVO.setMarket_wx_catlog_goods(goodsLog);
        String listLog = marketSystemMapper.selectValueById(15);
        configWxVO.setMarket_wx_catlog_list(listLog);
        String brand = marketSystemMapper.selectValueById(17);
        configWxVO.setMarket_wx_index_brand(brand);
        String topic = marketSystemMapper.selectValueById(18);
        configWxVO.setMarket_wx_index_topic(topic);
        String share = marketSystemMapper.selectValueById(5);
        configWxVO.setMarket_wx_share(share);
        return configWxVO;
    }

    @Override
    public Result wxUpdate(ConfigWxVO configWxVO) {
        // {2 10 12 15 17 18}

        String regexNum = "^\\d+$";
        System.out.println(configWxVO.getMarket_wx_share());
        Pattern pattern = Pattern.compile(regexNum);
        Matcher matcher1 = pattern.matcher(configWxVO.getMarket_wx_catlog_goods());
        Matcher matcher2 = pattern.matcher(configWxVO.getMarket_wx_catlog_list());
        Matcher matcher3 = pattern.matcher(configWxVO.getMarket_wx_index_brand());
        Matcher matcher4 = pattern.matcher(configWxVO.getMarket_wx_index_hot());
        Matcher matcher5 = pattern.matcher(configWxVO.getMarket_wx_index_new());
        Matcher matcher6 = pattern.matcher(configWxVO.getMarket_wx_index_topic());
        if (!matcher1.matches() || !matcher2.matches() || !matcher3.matches() || !matcher4.matches() ||
                !matcher5.matches() || !matcher6.matches()) {
            return Result.fail("数值格式有误。请输入正确的非负整数格式。");
        }
        marketSystemMapper.updateValueById(2, configWxVO.getMarket_wx_index_new());
        marketSystemMapper.updateValueById(10, configWxVO.getMarket_wx_index_hot());
        marketSystemMapper.updateValueById(12, configWxVO.getMarket_wx_catlog_goods());
        marketSystemMapper.updateValueById(15, configWxVO.getMarket_wx_catlog_list());
        marketSystemMapper.updateValueById(17, configWxVO.getMarket_wx_index_brand());
        marketSystemMapper.updateValueById(18, configWxVO.getMarket_wx_index_topic());
        marketSystemMapper.updateValueById(5,configWxVO.getMarket_wx_share());
        return Result.ok(configWxVO);
    }

}
